<template>
	<view>
		<detail-info :item="detail" @changeevent="updateData"></detail-info>
		<view class="u-comment-title">最新评论 {{comment.count}}</view>
		<view class="uni-comment u-comment">
			<block v-for="(item, index) in comment.list" :key="index">
				<comment-list :item="item"></comment-list>
			</block>
		</view>
		<view style="height: 120rpx;"></view>
		<!-- 输入框 -->
		<user-chat-bottom @submit="submit"></user-chat-bottom>
		
		<!-- 分享 -->
		<more-share @toggle="toggle" :show="shareshow" :sharedata="sharedata"></more-share>
	</view>
</template>

<script>
	import detailInfo from '../../components/detail/detail-info.vue'
	import time from '../../common/time.js'
	import commentList from '../../components/detail/comment-list.vue'
	import userChatBottom from '../../components/user-chat/user-chat-bottom.vue'
	import moreShare from '../../components/common/more-share.vue'
	export default {
		data() {
			return {
				sharedata: { // 分享内容
					title: '',
					url: '',
					titlepic: '',
					shareType: 0,
				},
				shareshow: false,
				detail: {
					userpic: '',
					username: '',
					sex: 1, // 0 男 1 女
					age: 0,
					content: '',
					isguanzhu: false,
					title: '',
					titlepic: '',
					morepic: [],
					video: false,
					share: false,
					path: '',
					sharenum: 0,
					commentnum: 0,
					goodnum: 0,
					create_time: 0
				},
				comment: {
					count: 0,
					list: []
				},
			}
		},
		components: {
			detailInfo,
			commentList,
			userChatBottom,
			moreShare,
		},
		onLoad(e) {
			this.initData(JSON.parse(e.detailData))
		},
		onNavigationBarButtonTap(e) { // 监听标题导航栏右边按钮点击事件
			if (e.index === 0) {
				this.toggle()
			}
		},
		methods: {
			updateData(data) {
				switch(data.type) {
					case 'guanzhu':
					this.updateGuanZhu(data)
						break
					case 'support':
					this.updateSupport(data)
						break
				}
			},
			updateGuanzhu(data) {
				this.detail.isguanzhu = data.data
			},
			updateSupport(data) { // 更新顶数据状态
				if (data.do == 'ding') {
					this.detail.infonum.index = 1
					this.detail.goodnum++
				}
			},
			__initShare(obj) { // 初始化分享内容
				this.sharedata = {
					title: obj.title,
					content: obj.title,
					url: 'https://dulily.gitee.io/',
					titlepic: obj.titlepic ? obj.titlepic : 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/app/share-logo@3.png',
					shareType: 0,
				}
			},
			initData(obj) { // 初始化数据
				// 修改窗口标题 API -> 界面 -> 设置导航条
				uni.setNavigationBarTitle({
				    title: obj.title
				});
				// 加载分享内容
				this.__initShare(obj)
				// 加载中
				uni.showLoading({ title: 'loading...', mask: true })
				obj.morepic = []
				obj.content = ''
				obj.goodnum = obj.infonum.dingnum
				this.detail = obj
				this.comment.count = obj.commentnum
				// 获取文章详情
				this.getdetail()
				if (this.comment.count) {
					this.getComment()
				}
			},
			async getdetail() { // 获取文章详情
				let [err, res] = await this.$http.get('/post/' + this.detail.id)
				let error = this.$http.errorCheck(err, res, ()=>{
					uni.hideLoading()
				}, ()=>{
					uni.hideLoading()
				})
				if (!error) return
				let data = res.data.data.detail
				this.detail.content = data.content
				let images = []
				data.images.forEach(ele => {
					images.push(ele.url)
				})
				this.detail.morepic = images
				this.detail.age = data.user.userinfo.age
				this.detail.sex = data.user.userinfo.sex
				return uni.hideLoading()
			},
			toggle() {
				this.shareshow = !this.shareshow
			},
			submit(data) {
				let obj = {
					id: 1,
					fid: 0,
					userpic: '../../static/logo.png',
					username: '昵称',
					time: time.gettime.gettime(new Date().getTime()),
					data: data
				}
				this.comment.list.push(obj)
			},
			async getComment() { // 获取评论数据
				let [err, res] = await this.$http.get('/post/' + this.detail.id + '/comment')
				if (!this.$http.errorCheck(err, res)) return
				let list = res.data.data.list
				this.comment.list = this.comment.list.concat(this.__ArrSort(list))
			},
			__ArrSort(arr, id=0) { // 评论排序
				let temp = []
				let lev = 0
				let forFn = function(arr, id, lev) {
					arr.forEach(ele => {
						let item = ele
						if (item.fid == id) {
							item.lev = lev
							temp.push({
								id: item.id,
								fid: item.fid,
								userid: item.user.id,
								userpic: item.user.userpic,
								username: item.user.username,
								time: item.create_time,
								data: item.data
							})
							forFn(arr, item.id, lev+1)
						}
					})
				}
				forFn(arr, id, lev)
				return temp
			},
		}
	}
</script>

<style lang="scss" scoped>
	// 评论
	.u-comment-title {
		padding: 20rpx;
		font-size: 30rpx;
		font-weight: bold;
	}
	.u-comment {
		padding: 0 20rpx;
	}
</style>
